<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="wpmp.security.client.utils.ButtonConfigUtils"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>操作类型管理</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
var store = new Ext.data.JsonStore({
	autoDestroy: true,
	autoLoad: true,
	url: '${ctx}/operation!listJSON.action',
   	root: 'records',
	fields: [
        {name: 'id',mapping: 'id',type: 'string'},
        {name: 'name',mapping: 'name',type: 'string'},
        {name: 'value',mapping: 'value',type: 'string'},
        {name: 'updateUser',mapping: 'updateUser',type: 'string'},
        {name: 'updateUserName',mapping: 'updateUserName',type: 'string'},
        {name: 'updateTime',mapping: 'updateTime',type: 'string'},
        {name: 'updateUser',mapping: 'updateUser',type: 'string'}
	]
});

var sm = new Ext.grid.CheckboxSelectionModel({
	singleSelect: false
});

var cm = new Ext.grid.ColumnModel({
	defaults: {
		align: 'center',
		sortable: true
	},
	columns: [
		new Ext.grid.RowNumberer(), 
		sm,
		{header: '操作名称',dataIndex: 'name',width: 15}, 
		{header: '关键词',dataIndex: 'value',width: 52},
		{header: '更新者',dataIndex: 'updateUser',width: 15,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return record.get('updateUserName');
			}
		},
		{header: '更新时间',dataIndex: 'updateTime',width: 18}
	]
});

var gridPanel = new Ext.grid.EditorGridPanel({
	style: 'text-align:left;',
	enableDragDrop: true,
	ddGroup: "GridDD",
	border: true,
	stripeRows: true,// 斑马线效果
	loadMask: {
		msg: '加载数据中, 请稍侯... ...'
	},
	trackMouseOver: true,
	store: store,
	cm: cm,
	sm: sm,
	clicksToEdit: 1,
	autoExpandColumn: 'value',
	viewConfig: {
		scrollOffset: 20,
		forceFit: true
	},
	tbar: [{
		text: '刷新',
		iconCls: "refreshBtn",
		handler: function() {
			store.reload();
		}
	},'-',{
		text: '新建',
		iconCls: "addBtn",
		hidden: <%=ButtonConfigUtils.hidden(request, response, "operation.list.add")%>,
		handler: function() {
			var url="${ctx}/operation!input.action?winId=winInsertOperation&U="+(new Date().getTime());
			windowOpen(url,'winInsertOperation','新建操作类型',255,355,'','',{
				onClose: function(scope){
					var obj=scope.returnValue;
					if(obj){
						store.reload();
					}
				}
			});
		}
	},'-',{
		text: '编辑',
		iconCls: "editBtn",
		hidden: <%=ButtonConfigUtils.hidden(request, response, "operation.list.edit")%>,
		handler: function() {
			if (sm.getCount()!=1) {
				Common.showErrorMsg('必须且仅能选择一条记录！');
				return;
			}
			var paraUrl="&id="+sm.getSelections()[0].get("id");
			var url="${ctx}/operation!input.action?winId=winUpdateOperation&U="+(new Date().getTime())+paraUrl;
			windowOpen(url,'winUpdateOperation','更新操作类型',255,355,'','',{
				onClose: function(scope){
					var obj=scope.returnValue;
					if(obj){
						store.reload();
					}
				}
			});
		}
	},'-',{
		text: '删除',
		iconCls: "deleteBtn",
		hidden: <%=ButtonConfigUtils.hidden(request, response, "operation.list.delete")%>,
		handler: function() {
			if(sm.getCount()==0) {
				Common.showErrorMsg('请至少选择一条记录！');
				return;
			}
			Ext.Msg.confirm("", "确认删除？",function(e) {
				if(e=='no') {
					return;
				}
				Common.ajaxRequest('${ctx}/operation!deleteJSON.action',{
					ids: Common.getSelectedProperties(sm,'id')
				},function(success) {
					if(success) {
						store.reload();
					}
				});
			});
		}
	}],
	listeners: {
		afterrender: function(thiz){
		    //拖动排序必须的代码
		    new Ext.dd.DropTarget(thiz.getEl(), {
		        ddGroup: "GridDD",
		        notifyDrop: function(dd,e,data) {
		            var rows=thiz.getSelectionModel().getSelections();
		            var count=rows.length;
		            var cindex=dd.getDragData(e).rowIndex;
		            var array=[];
		            for(var i=0;i<count;i++) {
		                var index=cindex+i;
		                array.push(index);
		            }
		            store.remove(store.getById(data.selections[0].id));
		            store.insert(cindex,data.selections); 

		            thiz.getView().refresh(); 
		            thiz.getSelectionModel().selectRows(array); 
		            
		            var ids=new Array();
		            for(var i=0;i<store.getCount();i++) {
		            	ids.push(store.getAt(i).get('id'));
		            }
		            Common.ajaxRequest('${ctx}/operation!sortJSON.action',{
						ids: ids
					},Ext.emptyFn,false);
		        }
		    });
		}
	}
});

Ext.onReady(function() {
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [gridPanel]
	});
});
</script>
</head>

<body>
</body>
</html>